<?php

namespace App\Services\Strator;

use App\Models\Strator\StratorProductType;

class StratorProductTypeService
{

    //
    public function getStratorProductTypes(): array{
        $stratorProductTypes=StratorProductType::with(['parent'])
            ->where('pid',0)
            ->orderBy('sort','asc')
            ->get()
            ->toArray();
        foreach ($stratorProductTypes as $key=>$productType){
            $stratorProductTypes[$key]['children']=$this->getStratorProductTypesByPid($productType['id']);
        }
        return $stratorProductTypes;
    }

    //
    public function getStratorProductTypesByPid($pid): array{
        $children=StratorProductType::with(['parent'])->where('pid',$pid)
            ->orderBy('sort','asc')
            ->get()->toArray();
        foreach ($children as $key=>$productType){
            $children[$key]['children']=$this->getStratorProductTypesByPid($productType['id']);
        }
        return $children;
    }

}
